﻿using Kingdee.BOS.Core.List.PlugIn;
using Kingdee.BOS.Core.List.PlugIn.Args;
using Kingdee.BOS.Core.Permission;
using Kingdee.BOS.Util;
using Kingdee.BOS.WebApi.FormService;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DevSamPle
{
    [Description("二开案例.列表插件.使用PrepareFilterParameter事件干预列表查询"), HotUpdate]
    public class ListPlugin : AbstractListPlugIn
    {
        public override void PrepareFilterParameter(FilterArgs e)
        {
            base.PrepareFilterParameter(e);
            //审核权限key 可以从T_SEC_PermissionItem表查
            string permissionItem = "47afe3d45bc84016b416a1206e121d45";
            PermissionAuthResult permissionAuthResult = this.View.Model.FuncPermissionAuth(
                    new string[] { }, permissionItem, null, false).FirstOrDefault();
            var isP = permissionAuthResult.Passed;
            var isa = permissionAuthResult.PermissionAuth;
            if (!isP)
            {
                var userId = this.Context.UserId;
                var result = WebApiServiceCall.ExecuteBillQuery(this.Context, CustCommon.QueryParameter("SEC_User", "FLinkObject.fname", $"fuserid={userId}"));
                string purchaserName = "";
                if (result.FirstOrDefault().FirstOrDefault() != null)
                {
                    purchaserName = result.FirstOrDefault().FirstOrDefault().ToString();
                }
                //string billno = "CGDD000029";
                //e.FilterString = e.FilterString.JoinFilterString($"fbillno='{billno}'");
                e.FilterString = e.FilterString.JoinFilterString($"FMaterialId.FPurchaserId='{purchaserName}'");
            } 
        }
    }
}
